package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.events.Event;

import java.util.List;
import java.util.Map;

@RestController
public class JDBCController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    //查询数据库的所有信息
    @RequestMapping("/userList")
    public List<Map<String, Object>> userList() {
        String sql = "select * from user";
        List<Map<String, Object>> list_maps = jdbcTemplate.queryForList(sql);
        return list_maps;
    }

    @RequestMapping("/addUser")
    public String addUser() {
        String sql = "insert into user (id,name,password) value (3,'小黑','123456')";
        jdbcTemplate.update(sql);
        return "数据插入成功";
    }

    @RequestMapping("/updateUser/{id}")
    public String updateUser(@PathVariable("id") String id) {
        String sql = "update user set name=?,password=? where id=" + id;
        Object[] objects = new Object[2];
        objects[0] = "小明";
        objects[1] = "12345";
        jdbcTemplate.update(sql,objects);
        return "数据修改成功";
    }

    @RequestMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") int id) {
        String sql = "delete from user where id=?";
        jdbcTemplate.update(sql,id);
        return "数据删除成功";
    }


}
